home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / UUCICO / NBSTIME.C < prev    next >
C/C++ Source or Header  |  1993-04-15  |  12KB  |  362 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    n b s t i m e . c                                               */
  3. /*                                                                    */
  4. /*    Set local system clock from National Bureau of Standards        */
  5. /*    Standard Time service                                           */
  6. /*--------------------------------------------------------------------*/
  7.  
  8. /*--------------------------------------------------------------------*/
  9. /*    Copyright (c) 1990-1993 by Kendra Electronic Wonderworks.       */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: NBSTIME.C 1.5 1993/04/15 04:15:43 ahd Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: NBSTIME.C $
  24.  * Revision 1.5  1993/04/15  04:15:43  ahd
  25.  * Twiddle OS/2 support and reduce number of #ifdef segments
  26.  *
  27.  * Revision 1.4  1993/04/13  02:27:31  dmwatt
  28.  * Windows/NT updates
  29.  *
  30.  * Revision 1.3  1993/04/11  00:34:11  ahd
  31.  * Global edits for year, TEXT, etc.
  32.  *
  33.  */
  34.  
  35. /*--------------------------------------------------------------------*/
  36. /*                        System include files                        */
  37. /*--------------------------------------------------------------------*/
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <time.h>
  42. #include <string.h>
  43.  
  44.  
  45. #ifdef WIN32
  46. #include <windows.h>
  47. #define NONDOS
  48.  
  49. #else
  50. #ifdef FAMILYAPI
  51. #define NONDOS
  52.  
  53. #define INCL_BASE
  54. #include <os2.h>
  55. #else /* FAMILYAPI */
  56. #ifndef __TURBOC__
  57. #include <dos.h>
  58. #endif /* __TURBOC__ */
  59. #endif /* FAMILYAPI */
  60. #endif /* WIN32 */
  61.  
  62.  
  63. /*--------------------------------------------------------------------*/
  64. /*                    UUPC/extended include files                     */
  65. /*--------------------------------------------------------------------*/
  66.  
  67. #include "lib.h"
  68. #include "arpadate.h"
  69. #include "dcp.h"
  70. #include "dcpsys.h"
  71. #include "hostable.h"
  72. #include "nbstime.h"
  73. #include "script.h"
  74. #include "security.h"
  75. #include "ulib.h"
  76.  
  77. #ifndef __TURBOC__
  78.    currentfile();
  79. #endif
  80.  
  81. /*--------------------------------------------------------------------*/
  82. /*    n b s t i m e                                                   */
  83. /*                                                                    */
  84. /*    Set system clock from using time from NBS of the format:        */
  85. /*                                                                    */
  86. /*                  MJD  YR MO DA  H  M  S ST S UT1 msADV         OTM */
  87. /*  nbs format-->  47511 88-12-16 06:03:44 00 0 -.1 045.0 UTC(NIST) * */
  88. /*  @ 1200 baud    47511 88-12-16 06:03:45 00 0 -.1 045.0 UTC(NIST) * */
  89. /*--------------------------------------------------------------------*/
  90.  
  91. boolean nbstime( void )
  92. {
  93.    char buf[BUFSIZ];
  94.    time_t today;
  95.    struct tm  tx;
  96.    int cycles = 15;
  97.    int dst= 0;
  98.    time_t delta;
  99.    char sync = '?';
  100.    struct tm *tp;
  101.  
  102.  
  103. #ifdef WIN32
  104.    SYSTEMTIME DateTime;
  105.    TOKEN_PRIVILEGES tkp;
  106.    HANDLE hToken;
  107.    USHORT rc;
  108. #endif
  109.  
  110. #ifdef FAMILYAPI
  111.    DATETIME DateTime;
  112.    USHORT rc;
  113. #endif
  114.  
  115. #ifndef NONDOS
  116. #ifndef __TURBOC__
  117.  
  118.    unsigned short rc;
  119.    struct dosdate_t ddate;
  120.    struct dostime_t dtime;
  121.  
  122. #endif /* __TURBOC__ */
  123. #endif /* NONDOS */
  124.  
  125.    memset( &tx , '\0', sizeof tx);        /* Clear pointers          */
  126.    if (!expectstr("MJD", 5, NULL )) /* Margaret Jane Derbyshire? :-) */
  127.    {
  128.       printmsg(0,"nbstime: Did not find MJD literal in data from remote");
  129.       return FALSE;
  130.    }
  131.  
  132.    rmsg(buf, 2, 2, sizeof buf);
  133.                   /* Read one line to get us setup for input   */
  134.  
  135. /*--------------------------------------------------------------------*/
  136. /*                  Begin main loop to get the time                   */
  137. /*--------------------------------------------------------------------*/
  138.  
  139.    while ((rmsg(buf, 2, 2, sizeof buf) != TIMEOUT) && cycles--)
  140.    {
  141.       sync = buf[ strlen( buf ) - 1 ];
  142.  
  143.       if (sync == '#')
  144.          break;
  145.       else if (sync != '*')
  146.          *buf = '\0';
  147.  
  148.    } /* while */
  149.  
  150.    if ( (cycles && (sync == '*')) || (*buf == '\0'))
  151.    {
  152.       printmsg(0,"nbstime: Did not get good buffer: \"%s\"", buf );
  153.                   return FALSE;
  154.    }
  155.  
  156. /*--------------------------------------------------------------------*/
  157. /*                   Determine the time we received                   */
  158. /*--------------------------------------------------------------------*/
  159.  
  160.    sscanf(buf,"%*s %d-%d-%d %d:%d:%d %d ",
  161.          &tx.tm_year, &tx.tm_mon, &tx.tm_mday ,
  162.          &tx.tm_hour, &tx.tm_min, &tx.tm_sec, &dst);
  163.    tx.tm_mon--;               /* Tm record counts months from zero   */
  164.  
  165.    today = mktime(&tx);       /* Current UTC (GMT) time in seconds   */
  166.  
  167.    if ( debuglevel > 2 )
  168.    {
  169.       printmsg(3,"%2d/%2d/%2d %2d:%2d:%2d %2d %c translates to %ld or %s",
  170.          tx.tm_year, tx.tm_mon + 1 , tx.tm_mday ,
  171.          tx.tm_hour, tx.tm_min, tx.tm_sec, dst, sync ,
  172.          today, ctime( &today ));
  173.    }
  174.  
  175. /*--------------------------------------------------------------------*/
  176. /*    Perform a sanity check; the time must be 20 years past 1970     */
  177. /*--------------------------------------------------------------------*/
  178.  
  179.    if ( today < 630720000L )
  180.    {
  181.       printmsg(0,"nbstime: Time warp error (%s), clock not set",
  182.             ctime( &today ));
  183.       return FALSE;
  184.    }
  185.  
  186. /*--------------------------------------------------------------------*/
  187. /*     Borland C++ doesn't set the time properly; do a conversion     */
  188. /*--------------------------------------------------------------------*/
  189.  
  190. #ifdef __TURBOC__
  191.    today -= timezone;
  192. #endif
  193.  
  194. #ifdef FAMILYAPI
  195.    today -= timezone;
  196. #endif
  197.  
  198. /*--------------------------------------------------------------------*/
  199. /*                        Set the system clock                        */
  200. /*--------------------------------------------------------------------*/
  201.  
  202.    tp = localtime(&today);    /* Get local time as a record          */
  203.  
  204. #ifdef FAMILYAPI
  205.  
  206.    rc = DosGetDateTime( &DateTime );
  207.    if ( rc != 0 )
  208.    {
  209.       printmsg(0,"Return code from DosGetDateTime %d", rc);
  210.       panic();
  211.    }
  212.  
  213.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  214.       (int) DateTime.year, (int) DateTime.month, (int) DateTime.day ,
  215.       (int) DateTime.hours, (int) DateTime.minutes,(int) DateTime.seconds ,
  216.       (int) DateTime.timezone, (int) DateTime.weekday );
  217.  
  218.    DateTime.year    = (USHORT) tp->tm_year + 1900;
  219.    DateTime.month   = (UCHAR) (tp->tm_mon + 1);
  220.    DateTime.day     = (UCHAR) tp->tm_mday;
  221.    DateTime.hours   = (UCHAR) tp->tm_hour;
  222.    DateTime.minutes = (UCHAR) tp->tm_min;
  223.    DateTime.seconds = (UCHAR) tp->tm_sec;
  224.  
  225.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  226.       (int) DateTime.year, (int) DateTime.month, (int) DateTime.day ,
  227.       (int) DateTime.hours, (int) DateTime.minutes,(int) DateTime.seconds ,
  228.       (int) DateTime.timezone, (int) DateTime.weekday );
  229.  
  230.    rc = DosSetDateTime( &DateTime );
  231.    if ( rc != 0 )
  232.    {
  233.       printmsg(0,"Return code from DosGetDateTime %d", rc);
  234.       panic();
  235.    }
  236.  
  237. #elif defined( WIN32 )
  238.  
  239.    GetSystemTime( &DateTime );
  240.  
  241.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d, weekday %d",
  242.       (int) DateTime.wYear, (int) DateTime.wMonth, (int) DateTime.wDay ,
  243.       (int) DateTime.wHour, (int) DateTime.wMinute,(int) DateTime.wSecond ,
  244.       (int) DateTime.wDayOfWeek );
  245.  
  246.    DateTime.wYear    = (WORD) tp->tm_year + 1900;
  247.    DateTime.wMonth   = (WORD) (tp->tm_mon + 1);
  248.    DateTime.wDay     = (WORD) tp->tm_mday;
  249.    DateTime.wHour    = (WORD) tp->tm_hour;
  250.    DateTime.wMinute  = (WORD) tp->tm_min;
  251.    DateTime.wSecond  = (WORD) tp->tm_sec;
  252.  
  253.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d, weekday %d",
  254.       (int) DateTime.wYear, (int) DateTime.wMonth, (int) DateTime.wDay ,
  255.       (int) DateTime.wHour, (int) DateTime.wMinute, (int) DateTime.wSecond ,
  256.       (int) DateTime.wDayOfWeek );
  257.  
  258.    if (!OpenProcessToken(GetCurrentProcess(),
  259.       TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  260.       &hToken))
  261.    {
  262.       printmsg(0, "nbstime: OpenProcessToken failed: error = %u\n",
  263.          GetLastError());
  264.       return FALSE;
  265.    }
  266.  
  267.    LookupPrivilegeValue(NULL, "SeSystemtimePrivilege",
  268.       &tkp.Privileges[0].Luid);
  269.    tkp.PrivilegeCount = 1;
  270.    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  271.  
  272.    if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  273.       (PTOKEN_PRIVILEGES)NULL, 0))
  274.    {
  275.       printmsg(0, "nbstime: first AdjustTokenPrivilege failed: returned %u\n",
  276.          GetLastError());
  277.       return FALSE;
  278.    }
  279.  
  280.    rc = SetSystemTime( &DateTime );
  281.    if ( !rc )
  282.    {
  283.       printmsg(0, "nbstime: SetSystemTime failed: returned %u\n",
  284.          GetLastError());
  285.    }
  286.  
  287.    tkp.Privileges[0].Attributes = 0;
  288.  
  289.    if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  290.       (PTOKEN_PRIVILEGES)NULL, 0))
  291.    {
  292.       printmsg(0, "nbstime: AdjustTokenPrivilege disable failed: returned %u\n",
  293.          GetLastError());
  294.       return FALSE;
  295.    }
  296.  
  297. #elif defined( __TURBOC__ )
  298.  
  299. /*--------------------------------------------------------------------*/
  300. /*    If this timezone uses daylight savings and we are in the        */
  301. /*    period to spring forward, do so.                                */
  302. /*--------------------------------------------------------------------*/
  303.  
  304.    if (daylight && ( dst > 1 ) && ( dst < 52 ))
  305.       today += 3600;          /* This is valid for the USA only      */
  306.    stime( &today );
  307.  
  308. #else /* __TURBOC__ */
  309.  
  310.    tp = localtime(&today);    /* Get local time as a record          */
  311.  
  312.    ddate.day     = (unsigned char) tp->tm_mday;
  313.    ddate.month   = (unsigned char) (tp->tm_mon + 1);
  314.    ddate.year    = (unsigned int)  (tp->tm_year + 1900);
  315.    ddate.dayofweek = (unsigned char) tp->tm_wDay;       /* 0-6, 0=Sunday */
  316.  
  317.    dtime.hour    = (unsigned char) tp->tm_hour;
  318.    dtime.minute  = (unsigned char) tp->tm_min;
  319.    dtime.second  = (unsigned char) tp->tm_sec;
  320.    dtime.hsecond = (unsigned char) 0;
  321.  
  322.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  323.       (int) ddate.year, (int) ddate.month, (int) ddate.day ,
  324.       (int) dtime.hour, (int) dtime.minute,(int) dtime.second ,
  325.       (int) timezone, (int) ddate.dayofweek );
  326.  
  327.    if ( (rc = _dos_settime( &dtime )) != 0 )
  328.    {
  329.       printmsg(0,"Return code from _dos_settime %d", rc);
  330.       panic();
  331.    }
  332.  
  333.    if ( (rc = _dos_setdate( &ddate )) != 0 )
  334.    {
  335.       printmsg(0,"Return code from _dos_setdate %d", rc);
  336.       panic();
  337.    }
  338.  
  339. #endif
  340.  
  341. /*--------------------------------------------------------------------*/
  342. /*             Print debugging information, if requested              */
  343. /*--------------------------------------------------------------------*/
  344.  
  345.    delta = today - time( NULL );
  346.    printmsg(2,"nbstime: \"%s\"", buf);
  347.    printmsg(2,"nbstime: Time delta is %ld seconds, zone offset %ld, \
  348. daylight savings %d",
  349.                   delta, timezone, dst );
  350.  
  351.    if ( sync == '*' )
  352.       printmsg(2,"Warning: Was unable to synchonize with NBS master");
  353.  
  354. /*--------------------------------------------------------------------*/
  355. /*                Announce new time, return to caller                 */
  356. /*--------------------------------------------------------------------*/
  357.  
  358.    printmsg(0,"nbstime: New system time is %s", arpadate());
  359.    return TRUE;
  360.  
  361. } /* nbstime */
  362.